package com.ruoyi.web;

import com.ruoyi.check.domain.*;
import com.ruoyi.check.service.*;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.*;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.function.Supplier;


@SpringBootTest
public class RuoYiApplication {
    @Autowired
    private IMyTableService myTableService;

    @Autowired
    private ISysProblemService sysProblemService;
    @Autowired
    private ISysAutomaticService iSysAutomaticService;
    @Autowired
    private IECheckListFormNameService ieCheckListFormNameService;

    @Autowired
    private IECheckListFormSubItemService ieCheckListFormSubItemService;
    @Autowired
    private IECheckListFormItemService ieCheckListFormItemService;
    @Autowired
    private IECheckListFormValueService ieCheckListFormValueService;

    public static  String t0001(Supplier<String> sup){
        return sup.get();
    }
    public static  void t0002(Consumer<String> con){
        con.accept(con.toString());
    }
    public static  void t0003  (Consumer<String> con1,Consumer<String> con2){
      con1.andThen(con2);
    }
    //判断
    public static  Boolean t0004  (String s, Predicate<String> pre){

       return pre.test(s);
    }

    @Test
    public void t0051() {
        //判断id
        Long copysql = myTableService.copysql();
        Integer integer = Integer.valueOf(Math.toIntExact(copysql));
        if(integer>=10000){
            //拷贝表
            Date date = new Date();
            SimpleDateFormat yyyyMMdd = new SimpleDateFormat("yyyyMMdd");
            //拷贝表
            String format = yyyyMMdd.format(date);
            myTableService.copybiao(format+"fuben");
            //拷贝完成截断表
            myTableService.jieduan();
        }
    }


    @Test
    public void t0052() {
        for (int i = 0; i < 20000; i++) {
            MyTable myTable = new MyTable();
            myTable.setData(i+"ss");
            myTable.setCreatedAt(new Date());
            myTableService.insertMyTable(myTable);
        }
    }
    @Test
    public void t005() {

        try {
            String filePath = "C:\\Users\\Lenovo\\Desktop\\text.xlsx";
            FileInputStream fs = new FileInputStream(filePath);

            //使用POI提供的方法得到excel的信息
            POIFSFileSystem fileSystem = new POIFSFileSystem(fs);
            HSSFWorkbook hssfWorkbook = new HSSFWorkbook(fileSystem);
            //获取到工作表，因为一个excel可能有多个工作表
            HSSFSheet InsertSheet = hssfWorkbook.getSheetAt(0);
            //获取第一行（excel中的行默认从0开始，所以这就是为什么，一个excel必须有字段列头），即，字段列头，便于赋值
            HSSFRow row = InsertSheet.getRow(0);
            //分别得到最后一行的行号，和一条记录的最后一个单元格
            System.out.println("最后一行的行 " + InsertSheet.getLastRowNum());

            //向文件中写入数据
            FileOutputStream out = new FileOutputStream(filePath);

            //指定行

            int lastRowNum = 6;
            for (int i = 0; i < 10; i++) {

            }
            //在指定行后追加数据
            row = InsertSheet.createRow(lastRowNum);

            //设置第一个（从0开始）单元格的数据
            row.createCell(0).setCellValue("小米");
            row.createCell(1).setCellValue(24);
            row.createCell(2).setCellValue("nan");
            row.createCell(3).setCellValue("nanjing");
            out.flush();
            hssfWorkbook.write(out);
            out.close();
        }catch (Exception e){

        }
    }





    //向已知表中插入数据，累计追加
//

    @Test
    public  void t001(){
        String s = t0001(() -> {
            return "liming";
        });

        t0003((i)->{

        },(x)->{

        });
        Boolean aBoolean = t0004(s, (i) -> {
            int length = s.length();
            return length > 5;
        });

        ArrayList<String> strings = new ArrayList<>();
        strings.stream()
                .filter(name->name.startsWith("zhang1"));


    }
    @Test
    public  void t0(){
        ECheckListFormName eCheckListFormName = new ECheckListFormName();
        List<ECheckListFormName> eCheckListFormNames = ieCheckListFormNameService.selectECheckListFormNameList(eCheckListFormName);
        eCheckListFormNames.forEach(i->{
            String formName = i.getFormName();
            System.out.println(formName);
        });
    }

    @Test
    public void t1() {
        List<String> columnNames = new ArrayList<>();
        columnNames.add("column1");
        columnNames.add("column2");
        columnNames.add("column3");
        columnNames.add("column4");
        columnNames.add("column5");


        Sysgeneration tableInfo = new Sysgeneration();
        tableInfo.setTableName("sys_lll");
        tableInfo.setColumnNames(columnNames);
        ArrayList list = new ArrayList<>();
        list.add(tableInfo);
        iSysAutomaticService.createTable(list);

    }


    @Test
    public void t2() {
        List<String> table = iSysAutomaticService.findTable();
        table.forEach(i -> {
            System.out.println(i);
        });
    }

    @Test
    public void t4(){
        File file = new File("../ruoyi-ui/src/api/check");
        //文件不为空
        File[] files = file.listFiles();
        for (File i :
                files) {
            String name = i.getName();
            System.out.println(name);
        }

    }



    @Test
    public void t3() throws IOException {
        //1.拷贝vue部分
        File filejs = new File("../code/yimeng/vue/api/check");
        // 路径 拿到js
        File[] js = filejs.listFiles();

        //路径 vue


        File filevue = new File("../code/yimeng/vue/views/check/");
        File[] vue = filevue.listFiles();
        File[] v = vue[0].listFiles();
        //拿java部分
        File filejava = new File("../code/yimeng/main/java/com/ruoyi/check/");
        List<File> show = show(filejava);
        File filexml = new File("../code/yimeng/main/resources/mapper/check/");
        //第一条记录Controller
        File Controller = show.get(0);

        //第二条记录domain
        File domain = show.get(1);

        //mapper
        File mapper = show.get(2);
        //service
        File service = show.get(3);
        //xml
        File[] filesxml = filexml.listFiles();
        File filex = filesxml[0];



        System.out.println(js[0]);
        System.out.println(v[0]);
        String path = v[0].getPath();
        //path.substring()
        System.out.println(filex);
        System.out.println(Controller);
        System.out.println(domain);
        System.out.println(mapper);
        System.out.println(service);
        String substring = path.substring(0, path.length() - 15);
        String sourceFolderPath = substring;

      //  System.out.println(sourceFolderPath+"vvvvvvvvvvvvvvvvvvvvvvvvvvv");
       String targetFolderPath = "../ruoyi-ui/src/views/check";
////        //将将test下的文件复制到亿蒙下面
       copyFolder(sourceFolderPath, targetFolderPath);



       // System.out.println(substring);

//        File file = new File(substring);
//        File file1 = new File("../ruoyi-ui/src/api/views/check/");
//
//        File file2 = new File("D:\\yimeng\\test\\1");
//        File file3 = new File("D:\\yimeng\\test");




        //test vue


        //2.vue
/*
        Path sourcePathvue = Paths.get(substring);
        Path targetDirPathvue = Paths.get("../ruoyi-ui/src/api/views/check");
        // 构造目标文件路径
        Path targetPathvue = targetDirPathvue.resolve(sourcePathvue.getFileName());
        // 使用Files.copy()方法进行文件复制

        try {
            Files.copy(sourcePathvue, targetPathvue);
        } catch (IOException e) {
            throw new RuntimeException(e+"zipcontroller中的copy");
        }*/















        //将对应的文件写到指定位置
        //1.将文件复制到指定目录
       // File file = new File();

//       Path sourcePathjs = Paths.get(js[0].getPath());
//        Path targetDirPathjs = Paths.get("../ruoyi-ui/src/api/check");
//
//        // 构造目标文件路径
//        Path targetPath = targetDirPathjs.resolve(sourcePathjs.getFileName());
//
//        // 使用Files.copy()方法进行文件复制
//        Files.copy(sourcePathjs, targetPath);
//
//        //2.拷贝页面
//        Path pathvue =Paths.get(v[0].getPath());
//
//        Path targetDirPathvue = Paths.get("../ruoyi-ui/src/views/check/table");
//
//        Files.copy(pathvue,targetDirPathvue);




    }
    private static void copyFileUsingFileChannels(File source, File dest) throws IOException {
        FileChannel inputChannel = null;
        FileChannel outputChannel = null;
        try {
            inputChannel = new FileInputStream(source).getChannel();
            outputChannel = new FileOutputStream(dest).getChannel();
            outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
        } finally {
            inputChannel.close();
            outputChannel.close();
        }

    }
    private static void copyFileUsingFileStreams(File source, File dest)
            throws IOException {
        InputStream input = null;
        OutputStream output = null;
        try {
            input = new FileInputStream(source);
            output = new FileOutputStream(dest);
            byte[] buf = new byte[1024];
            int bytesRead;
            while ((bytesRead = input.read(buf)) > 0) {
                output.write(buf, 0, bytesRead);
            }
        } finally {
            input.close();
            output.close();
        }
    }
    public static List<File> show(File file) {//data
        ArrayList<File> strings = new ArrayList<>();

        File[] filesj = file.listFiles();//获取data下所有内容的对象
        for (File f : filesj) {//获取一个一个的对象可能是目录课可能是文件
            if (f.isFile()) {
                boolean b = f.getName().endsWith(".java");
                if (b) {
                    strings.add(f);
                }

            } else {//是目录
                File[] filesx = f.listFiles();
                for (File i:
                        filesx) {
                    boolean b = i.getName().endsWith(".java");
                    if (b) {
                        strings.add(i);
                    }
                }

            }
        }
        return strings;
    }

    public static void copyFile(String sourceFilePath, String targetFilePath) throws IOException {
        File sourceFile = new File(sourceFilePath);
        File targetFile = new File(targetFilePath);
        FileInputStream inputStream = new FileInputStream(sourceFile);
        FileOutputStream outputStream = new FileOutputStream(targetFile);
        byte[] buffer = new byte[4096];
        int length = 0;
        while ((length = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, length);
        }
        inputStream.close();
        outputStream.close();
    }
    public static void copyFolder(String sourceFolderPath, String targetFolderPath) {
        File sourceFolder = new File(sourceFolderPath);
        String[] sourceFilePathList = sourceFolder.list();
        File targetFolder = new File(targetFolderPath);
        //判断目标文件夹是否存在,没有则创建
        if (!targetFolder.exists()) {
            targetFolder.mkdirs();
        }
        try {
            for (String filePath : sourceFilePathList) {
                if (new File(sourceFolderPath + sourceFolder.separator + filePath).isDirectory()) {
                    copyFolder(sourceFolderPath + sourceFolder.separator + filePath, targetFolderPath + sourceFolder.separator + filePath);
                }
                if (new File(sourceFolderPath + sourceFolder.separator + filePath).isFile()) {
                    copyFile(sourceFolderPath + sourceFolder.separator + filePath, targetFolderPath + sourceFolder.separator + filePath);
                }
            }
        } catch (IOException e) {
            System.out.println("复制文件夹信息出错");
        }
    }




    @Test
    public void t5(){
        List<ECheckListFormItem> findtimu = ieCheckListFormItemService.findtimu("一号线");
        int size = findtimu.size();
        System.out.println(size);


    }
    @Test
    public void t6() throws IOException {

       //
        File filex = new File("../code/yimeng/main/resources/mapper/check/发动机转子检测Mapper.xml");
       /*  Path pathxml = Paths.get(filex.getPath()).getFileName();
        Path targetDirPathxml = Paths.get("../ruoyi-check/src/main/resources/mapper/check");

        Files.copy(pathxml, targetDirPathxml);*/

        Path sourcePathjs = Paths.get(filex.getPath());

        Path targetDirPathjs = Paths.get("../ruoyi-check/src/main/resources/mapper/check");

        // 构造目标文件路径

        Path targetPathjs = targetDirPathjs.resolve(sourcePathjs.getFileName());

        Files.copy(sourcePathjs, targetPathjs);

    }
}
